package com.summary.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.summary.domain.dto.TbSummaryDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.summary.mapper.TbSummaryMapper;
import com.summary.domain.TbSummary;
import com.summary.service.ITbSummaryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 日报业务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-15
 */
@Service
public class TbSummaryServiceImpl implements ITbSummaryService {
    @Resource
    private TbSummaryMapper tbSummaryMapper;

    /**
     * 查询日报业务
     *
     * @param id 日报业务主键
     * @return 日报业务
     */
    @Override
    public TbSummary selectTbSummaryById(Long id) {
        return tbSummaryMapper.selectTbSummaryById(id);
    }

    /**
     * 根据条件进行分页查询
     *
     * @param tbSummary
     * @return
     */

    @Override
    public List<TbSummary> selectTbSummaryListByPage(TbSummary tbSummary) {
        return tbSummaryMapper.selectTbSummaryByPage(tbSummary);
    }

    /**
     * 查询日报业务列表
     *
     * @param tbSummary 日报业务
     * @return 日报业务
     */
    @Override
    public List<TbSummary> selectTbSummaryList(TbSummary tbSummary) {
        return tbSummaryMapper.selectTbSummaryList(tbSummary);
    }

    /**
     * 新增日报业务
     *
     * @param tbSummary 日报业务
     * @return 结果
     */
    @Override
    public int insertTbSummary(TbSummary tbSummary) {
        tbSummary.setCreateTime(DateUtils.getNowDate());
        return tbSummaryMapper.insertTbSummary(tbSummary);
    }

    /**
     * 修改日报业务
     *
     * @param tbSummary 日报业务
     * @return 结果
     */
    @Override
    public int updateTbSummary(TbSummary tbSummary) {
        return tbSummaryMapper.updateTbSummary(tbSummary);
    }

    /**
     * 批量删除日报业务
     *
     * @param ids 需要删除的日报业务主键
     * @return 结果
     */
    @Override
    public int deleteTbSummaryByIds(Long[] ids) {
        return tbSummaryMapper.deleteTbSummaryByIds(ids);
    }

    /**
     * 删除日报业务信息
     *
     * @param id 日报业务主键
     * @return 结果
     */
    @Override
    public int deleteTbSummaryById(Long id) {
        return tbSummaryMapper.deleteTbSummaryById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult saveDaily(TbSummaryDto tbSummaryDto) {
        AjaxResult result = new AjaxResult();
        if (StringUtils.isBlank(tbSummaryDto.getYear())
                || StringUtils.isBlank(tbSummaryDto.getMonth())) {
            return AjaxResult.error("请填写时间");
        }
        Long userId = SecurityUtils.getUserId();
        Integer type = tbSummaryDto.getType() == null ? 1 : tbSummaryDto.getType();
        TbSummary daily = new TbSummary();
        if (type == 1) {
            if (StringUtils.isBlank(tbSummaryDto.getDay())) {
                return AjaxResult.error("请填写日期");
            } else {
                daily = tbSummaryMapper.getDailyById(userId, tbSummaryDto.getYear(), tbSummaryDto.getMonth(), tbSummaryDto.getDay());
                if (daily != null) {
                    return AjaxResult.error("今日已填写日报");
                }
            }

        } else {
            daily = tbSummaryMapper.getMonthById(userId, tbSummaryDto.getYear(), tbSummaryDto.getMonth());
            if (daily != null) {
                return AjaxResult.error("已填写月报");
            }
        }
        daily = new TbSummary();
        BeanUtils.copyProperties(tbSummaryDto, daily);
        daily.setUserId(userId);
        daily.setCreateTime(DateUtils.getNowDate());
        return tbSummaryMapper.insertDaily(daily) == 1 ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");


    }

    @Override
    public AjaxResult searchByDate(TbSummaryDto tbSummary) {
        String username = tbSummary.getUsername();
        String year = tbSummary.getYear();
        String month = tbSummary.getMonth();
        String day = tbSummary.getDay();
        TbSummaryDto result = tbSummaryMapper.searchByDate(username, year, month, day);
        System.out.println(result);
        if (result != null)
            return AjaxResult.success(result);
        else
//            return AjaxResult.error("查询数据为空", "");
            return AjaxResult.error(202,"查询失败");
    }


    @Override
    public AjaxResult saveLevel(TbSummaryDto tbSummaryDto) {
        AjaxResult result = new AjaxResult();
        Long id = tbSummaryDto.getId();
        int level = tbSummaryDto.getLevel();
        TbSummary daily = new TbSummary();
        daily.setLevel(level);
        daily.setId(id);

        return tbSummaryMapper.saveLevel(daily) == 1 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult getDailyMonthList(TbSummaryDto tbSummaryDto) {
        AjaxResult result = new AjaxResult();
        Long userId = SecurityUtils.getUserId();
        String month = tbSummaryDto.getMonth();
        List<TbSummary> monthList = new ArrayList<>();
        monthList = tbSummaryMapper.getMonthByUser(userId, month);
        if (monthList.size() != 0)
            return AjaxResult.success(monthList);
        else
            return AjaxResult.error("月报为空");
    }

    /**
     * 首页一个月存在日报的列表
     *
     * @return 月内已填写的列表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult getListByIdYearMonthType(TbSummaryDto tbSummaryDto) {
        AjaxResult result = new AjaxResult();
        Long user_id = SecurityUtils.getUserId();
        TbSummary tb = new TbSummary();
        tb.setUserId(user_id);
        tb.setYear(tbSummaryDto.getYear());
        tb.setMonth(tbSummaryDto.getMonth());
        tb.setType(tbSummaryDto.getType());
        List<TbSummary> list = tbSummaryMapper.getListByIdYearMonthType(tb);
        return AjaxResult.success(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult update(TbSummaryDto tbSummaryDto) {
        AjaxResult result = new AjaxResult();
        if (tbSummaryDto.getId() == null) {
            return AjaxResult.error(203, "请选择日报");
        }
        int type = tbSummaryDto.getType() == null ? 1 : tbSummaryDto.getType();
        TbSummary tbSummary = new TbSummary();
        BeanUtils.copyProperties(tbSummaryDto, tbSummary);

        return tbSummaryMapper.updateTbSummary(tbSummary) == 1 ? AjaxResult.success("修改成功") : AjaxResult.error(203, "修改失败");
    }

    //    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public AjaxResult getAllDailyMonthList(TbSummaryDto tbSummaryDto) {
//        AjaxResult result = new AjaxResult();
//        List<TbSummary> monthList = new ArrayList<>();
//        monthList = tbSummaryMapper.getMonthByMonth(tbSummaryDto);
//        if (monthList.size()!=0)
//            return AjaxResult.success(monthList);
//        else
//            return AjaxResult.error();
//    }
    @Override
    public List<TbSummary> getAllDailyMonthList(TbSummaryDto tbSummaryDto) {
        return tbSummaryMapper.getMonthByMonth(tbSummaryDto);
    }


    /**
     * 查询周报
     * @param tbSummaryDto
     * @return
     */
    @Override
    public AjaxResult getWeekListByUserId(TbSummaryDto tbSummaryDto) {
        //1.取出userId，如果为管理员，则查看所有的
        Long userId = tbSummaryDto.getUserId();
        if (userId == 1 || userId == 2) {
            List<TbSummary> allWeakSummary = tbSummaryMapper.getAllWeakSummary(tbSummaryDto);
            return AjaxResult.success(allWeakSummary);
        }
        //2.如果不为管理员，则查看单个用户的
        else {
            List<TbSummary> weakSummaryById = tbSummaryMapper.getWeakSummaryById(tbSummaryDto);
            return AjaxResult.success(weakSummaryById) ;
        }
    }
}
